/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.TreeSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Dep;
import model.Med;
import model.Pays;
import model.Specialite;

/**
 *
 * @author sio
 */
public class Control extends HttpServlet {

    private model.Pays p;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        p = new Pays();
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String uc = request.getParameter("uc");
        String departement = request.getParameter("departement");
        String Dnom = request.getParameter("Dnom");
        String spe = request.getParameter("spe");
        String page;
        if (uc == null) {
            uc = "default";
        }
        switch (uc) {

            case "nom":
                if (Dnom == null) {
                    page = "med.jsp";
                    
                } else {
                    Collection<Med> LesMeds;
                    Collection<Med> lesVraiMed = new TreeSet<Med>();
                    Collection<Dep> lesDeps0 = p.getLesDeps();
                    for (Dep unDeparte : lesDeps0) {
                        LesMeds = unDeparte.getLesMeds();
                        for (Med unMed : LesMeds) {
                            String nomMed = unMed.getNom();
                            if (nomMed.startsWith(Dnom)) {
                                lesVraiMed.add(unMed);
                            }
                        }
                    }
                    request.setAttribute("listeVraiMed",lesVraiMed);
                    page = "mednom.jsp";
                   
                }
                request.getRequestDispatcher(page).forward(request, response);
                break;

            case "dep":

                if (departement == null) {
                    Collection<Dep> Deps = p.getLesDeps();
                    request.setAttribute("listeDep", Deps);
                    page = "dep.jsp";
                   

                } else {

                    Dep Departs = p.getLeDep(departement);
                    Collection<Med> Meds = Departs.getLesMeds();
                    request.setAttribute("listeMed", Meds);
                    page = "meddep.jsp";
                    
                }
                 request.getRequestDispatcher(page).forward(request, response);
                break;
                
            case "spe" :
                if (spe == null){
                    Collection <Specialite> Specs = p.getLesSpecs();
                    request.setAttribute("listeSpe", Specs);
                    page = "spe.jsp";
                   
                }else{
                     Specialite specialite = p.getLaSpe(spe);
                    Collection<Med> Meds = specialite.getLesMeds();
                    request.setAttribute("listeMed", Meds);
                    page = "medspe.jsp";
                }
                 request.getRequestDispatcher(page).forward(request, response);
               
                break;
                
            default:

                page = "index.jsp";
                request.getRequestDispatcher(page).forward(request, response);
                break;

        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
